package com.lsp.rabbitmqcousumer.direct;

import com.lsp.rabbitmqcousumer.constans.MQConstants;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 延迟消息消费者(插件形式的死信队列)
 * 插件形式的死信队列 是在Exchange配置的参数，所以exchange是死信交换机
 * 不用插件配置的死信队列 是在Queue配置的参数，所以Queue是死信队列
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(
                        value = MQConstants.DELAY_QUEUE,
                        durable = "true"
                ),
                exchange = @Exchange(
                        value = MQConstants.DELAY_EXCHANGE,
                        type = ExchangeTypes.DIRECT,
                arguments = @Argument(name = "x-delayed-type",value = "direct"),delayed = "true"),
                key =  MQConstants.DELAY_KEY)
)
public class DelayedReceiver {

    @RabbitHandler(isDefault = true)
    public void receiverDelay(String msg, Message message, Channel channel){
        System.out.println(message.toString());
        System.out.println(message.getMessageProperties().getReceivedDelay());
        System.out.println("----------收到消息：" + msg + "当前时间：" + LocalDateTime.now());
    }
}
